Skip to content

Clarify WindowsMut (Lending)Iterator#135773

Merged
bors merged 1 commit into
rust-lang:masterfrom
hkBst:patch-10
Jan 27, 2025
Merged

Clarify WindowsMut (Lending)Iterator#135773
bors merged 1 commit into
rust-lang:masterfrom
hkBst:patch-10

Conversation

@hkBst

@hkBst hkBst commented Jan 20, 2025

Copy link
Copy Markdown
Member

fixes #133628

@rustbot

rustbot commented Jan 20, 2025

Copy link
Copy Markdown
Collaborator

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 20, 2025
@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/slice/mod.rs Outdated
Comment on lines +1102 to +1103
/// There's no `windows_mut` Iterator-equivalent of `windows`,
/// (as `[0,1,2].windows_mut(2).collect()` would create 2 mutable references to the middle elements of that array),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an example, I think it may be worth giving the reasoning here. For example, "... because the Iterator trait cannot represent the required lifetimes (though this is possible with a [LendingIterator]). However, you can sometimes..."

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now lead with this more abstract lifetime reasoning, though I did not cut the counter(!)example, as I think it is a simple to understand but powerful proof of non-existence of windows_mut as an Iterator. Let me know what you think!

@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/slice/mod.rs Outdated
Comment on lines +1102 to +1106
/// Because the [Iterator] trait cannot represent the required lifetimes, there is no `windows_mut` analog to `windows`.
/// (If there was, then `[0,1,2].windows_mut(2).collect()` would create 2 mutable references
/// to the middle element of that array, which would violate [the rules of references],
/// though a [LendingIterator] analog is possible.)
/// However, you can sometimes use [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think things can be simplified a bit: complete thoughts shouldn't be in ( ... ), and I don't think we need to point link to the reference docs (we can probably assume users know that two mutable references to the same thing is bad). So suggested wording, also dropping the LendingIterator mention:

Suggested change
/// Because the [Iterator] trait cannot represent the required lifetimes, there is no `windows_mut` analog to `windows`.
/// (If there was, then `[0,1,2].windows_mut(2).collect()` would create 2 mutable references
/// to the middle element of that array, which would violate [the rules of references],
/// though a [LendingIterator] analog is possible.)
/// However, you can sometimes use [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
/// There is no `windows_mut` analog to `windows` because [`Iterator`] does not represent the
/// required lifetimes, e.g. `[0, 1, 2].windows_mut(2).collect()` would allow creating two mutable
/// references to the middle element of the array. However, you can sometimes use
/// [`Cell::as_slice_of_cells`] (crate::cell::Cell::as_slice_of_cells) in

Then add something like "This pattern can also be achieved with a [LendingIterator]." after the example (to keep the paragraph introducing the example only about std).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding complete thought in parens: The second site I looked up gave this example: Tonight's gala is a huge occasion. (Everyone who's somebody will be there.), so I don't quite see what is wrong with my use of parens here. I've used the parens in the sense that you can get more information by reading what's inside them, but you can also safely skip over them, and this way the reference outside of std is not in the unparenthesized text. Upon further reflection I suppose I could move the parens to only go around "though a [LendingIterator] analog is possible".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the parenthetical more terse and adjusted punctuation. Let me know if you think it helps.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is grammatically correct, but readability tends to be better if parenthesized data is kept brief. If there are complete asides that need to be expressed, it is preferable to put it in a footnote or separate paragraph, or just fold it into the text somehow.

Reword looks fine though 👍

@hkBst hkBst Jan 27, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! :D

@tgross35 tgross35 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the changes! Please just rewrap the comments to 100 characters and squash.

@hkBst hkBst force-pushed the patch-10 branch 2 times, most recently from 01f49b0 to cc52066 Compare January 27, 2025 09:34
@hkBst

hkBst commented Jan 27, 2025

Copy link
Copy Markdown
Member Author

@tgross35 I've rewrapped text and squashed, but there is one long line with the link to the book that I don't know how to shorten, and am afraid to break up. Let me know if there is anything that can be done there, otherwise I think this is good to go.

Comment on lines +1102 to +1107
/// Because the [Iterator] trait cannot represent the required lifetimes,
/// there is no `windows_mut` analog to `windows`;
/// `[0,1,2].windows_mut(2).collect()` would violate [the rules of references]
/// (though a [LendingIterator] analog is possible).
/// You can sometimes use
/// [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
/// conjunction with `windows` instead:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Because the [Iterator] trait cannot represent the required lifetimes,
/// there is no `windows_mut` analog to `windows`;
/// `[0,1,2].windows_mut(2).collect()` would violate [the rules of references]
/// (though a [LendingIterator] analog is possible).
/// You can sometimes use
/// [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
/// conjunction with `windows` instead:
/// Because the [Iterator] trait cannot represent the required lifetimes, there is no
/// `windows_mut` analog to `windows`; `[0,1,2].windows_mut(2).collect()` would violate
/// [the rules of references] (though a [LendingIterator] analog is possible). You can sometimes
/// use [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in conjunction with
/// `windows` instead:

Looks like it may have gotten chopped up somehow, it should be something like the wrapping here (I wish rustfmt just did this...)

No worries about links, those are find to exceed 100.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did manual line breaking at logical places, which is why it looks a bit ragged.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense. We tend to avoid semantic line breaks in docs because it's a small benefit for reading diffs but makes reading the code less nice, and the code gets read significantly more than diffs do.

Anyway, thanks for all the follow up here!

@tgross35

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@bors

bors commented Jan 27, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit f630f7f has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 27, 2025
@hkBst

hkBst commented Jan 27, 2025

Copy link
Copy Markdown
Member Author

@tgross35 thanks for reviewing!

@bors bors merged commit b7476f1 into rust-lang:master Jan 27, 2025
@rustbot rustbot added this to the 1.86.0 milestone Jan 27, 2025
@hkBst hkBst deleted the patch-10 branch February 2, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarify statement regarding windows_mut

5 participants